package tylerp.dnd;

import tylerp.dnd.util.Random;

public class Roller {

    /**
     * This method simulates dice rolling based on the parameters given, but
     * it returns the results in a Roll object.
     * @param numDice an int specifying how many dice are to be rolled
     * @param numSides an int specifying how many sides are on the dice
     * @param modif an int specifying how to modify the final result
     * @return a Roll object containing all information about the roll
     */
    public static Roll rollToRoll(int numDice, int numSides, int modif) {

        Random rand = new Random();
        int[] rolls = new int[numDice];

        for (int i = 0; i < numDice; i++) {
            int temp = rand.nextInt(numSides);
            rolls[i] = temp;
        }

        return new Roll(numDice, numSides, modif, rolls);
    }

    /**
     * This method simulates dice rolling based on the parameters given.
     * 
     * @param numDice an int specifying how many dice are to be rolled
     * @param numSides an int specifying how many sides are on the dice
     * @param modif an int specifying how to modify the final result
     * @return the formatted String representing this roll
     */
    public static String roll(int numDice, int numSides, int modif) {
        Random rand = new Random();
        String builder = "";

        // If more than 1 die, specify how many
        if (numDice > 1) {
            builder += numDice;
        }

        // Specify what kind of die is used
        builder += "d" + numSides;

        // If there is a modifier, specify
        if (modif > 0) {
            builder += "+" + modif;
        } else if (modif < 0) {
            builder += modif;
        }

        builder += "=";

        // If more than one die is being thrown, put the rolls in brackets
        if (numDice > 1) {
            builder += "[";
        }

        // Roll numbers, and print out results
        int total = 0;
        for (int i = 0; i < numDice; i++) {
            int temp = rand.nextInt(numSides);
            total += temp;
            builder += temp;
            if (i < numDice - 1) {
                builder += ",";
            }
        }

        // Close the brackets if you used them
        if (numDice > 1) {
            builder += "]";
        }

        // If you had a modifier, print it again, but update the total this time
        if (modif > 0) {
            builder += "+" + modif;
            total += modif;
        } else if (modif < 0) {
            builder += modif;
            total += modif;
        }

        // Show a grand total if more than one die is used or if there was a modifier
        if (numDice > 1 || modif != 0) {
            builder += "=" + total;
        }

        return builder;
    }
}

